﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfUserManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Person SelectedPerson
        {
            get
            {
                if (lbPersons.SelectedIndex == -1)
                {
                    lbPersons.SelectedIndex = 0;
                }

                return new Person();

                
            }
        }

        private BindingList<Person> persons = new BindingList<Person>() 
        {
            new Person() 
            { 
                Id = Guid.NewGuid(),
                FirstName="John",
                LastName="Smith", 
                BirthDate = DateTime.Parse("2 Jul 1980") 
            },
            new Person() 
            { 
                Id = Guid.NewGuid(),
                FirstName="John",
                LastName="Doe", 
                BirthDate = DateTime.Parse("5 Sep 1970")
            }
        };
        public MainWindow()
        {
            InitializeComponent();

            using (var context = new WpfUsersEntities())
            {
                lbPersons.ItemsSource = context.Users.ToList();
            }

            
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            using (var context = new WpfUsersEntities())
            {
                context.Users.Add(new User() {
                    Id = Guid.NewGuid(),
                    FirstName = txtFirstName.Text,
                    LastName = txtLastName.Text,
                    BirthDate = calendarBirthDate.SelectedDate
                });

                context.SaveChanges();

                lbPersons.ItemsSource = null;
                lbPersons.ItemsSource = context.Users.ToList();

            }
            
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            

            if (lbPersons.SelectedIndex == -1)
            {
                lbPersons.SelectedIndex = 0;
            }

            using (var context = new WpfUsersEntities())
            {
                var selectedUser = lbPersons.SelectedItem as User;
                var user =  context.Users.FirstOrDefault(u => u.Id == selectedUser.Id);

                user.FirstName = txtFirstName.Text;
                user.LastName = txtLastName.Text;
                user.BirthDate = calendarBirthDate.SelectedDate;

                context.SaveChanges();

                lbPersons.ItemsSource = null;
                lbPersons.ItemsSource = context.Users.ToList();
            }

        }


        private void lbPersons_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(lbPersons.SelectedIndex == -1 && lbPersons.Items.Count != 0)
            {
                lbPersons.SelectedIndex = 0;
            }

            using (var context = new WpfUsersEntities())
            {
                var selectedUser = lbPersons.SelectedItem as User;
                User user;

                if(selectedUser == null)
                {
                    user = new User();
                }
                else
                {
                    user = context.Users.FirstOrDefault(u => u.Id == selectedUser.Id);
                }
                

                txtId.Text = user.Id.ToString();
                txtFirstName.Text = user.FirstName;
                txtLastName.Text = user.LastName;
                calendarBirthDate.SelectedDate = user.BirthDate;

            }

            
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if(lbPersons.SelectedIndex == -1)
            {
                return;
            }

            using (var context = new WpfUsersEntities())
            {


                var selectedUser = lbPersons.SelectedItem as User;
                var user = context.Users.FirstOrDefault(u => u.Id == selectedUser.Id);

                context.Users.Remove(user);

                context.SaveChanges();

                lbPersons.ItemsSource = null;
                lbPersons.ItemsSource = context.Users.ToList();
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
                     
        }
    }
}
